//
//  CMDBPhotoBrowserViewCell.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/4.
//  Copyright © 2017年 CMDB. All rights reserved.
//

class CMDBPhotoBrowserViewCell: UICollectionViewCell {
    // MARK:- 定义属性
    var picURL : CMDBPictureMdeol? {
        didSet {
            
            setupContent(picURL)
        }
    }
    
    // MARK:- 懒加载属性
    private lazy var scrollView : UIScrollView = UIScrollView()
    private lazy var imageView : UIImageView = UIImageView()
    
    // MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


// MARK:- 设置UI界面内容
extension CMDBPhotoBrowserViewCell {
    private func setupUI() {
        // 1.添加子控件
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        
        // 2.设置子控件frame
        scrollView.frame = contentView.bounds
    }
}


extension CMDBPhotoBrowserViewCell {
    
    private func setupContent(picURL : CMDBPictureMdeol?){
        
        // 1.nil值校验
        guard let picURL = picURL else {
            return
        }
        
        // 2.取出image对象
        
        // 3.计算imageView的frame
        let width = CMDB_Screen_Width
        let height = width / CGFloat(String.stringToInt(picURL.width)) * CGFloat(String.stringToInt(picURL.height))
        var y : CGFloat = 0
        if height > CMDB_Screen_Height {
            y = 0
        } else {
            y = (CMDB_Screen_Height - height) * 0.5
        }
        imageView.frame = CGRect(x: 0, y: y, width: width, height: height)
        
        // 4.设置imagView的图片
        imageView.loadPicImage(picURL.url)
        
        //
        scrollView.contentSize =  CGSize(width: 0, height: height)
        
    }
    
    private func getBigURL(smallPicUrl:String) -> String{
    
        let bigURLString = smallPicUrl.stringByReplacingOccurrencesOfString("small", withString: "big")
        
        return bigURLString
        
    }
    
}